<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import $api from '@/api/api'
import { Lock, InfoFilled } from '@element-plus/icons-vue'

// 密码表单数据
const passwordForm = reactive({
  password: '',
  confirmPassword: ''
})

// 表单校验规则
const rules = {
  password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const formRef = ref(null)
const loading = ref(false)

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    loading.value = true

    const res = await $api.updatePassword({ password: passwordForm.password })
    if (res.code === 1) {
      ElMessage.success('密码修改成功')
      // 重置表单
      passwordForm.password = ''
      passwordForm.confirmPassword = ''
      formRef.value.resetFields()
    } else {
      ElMessage.error(res.msg || '密码修改失败')
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('密码修改失败')
    }
  } finally {
    loading.value = false
  }
}

// 重置表单
const handleReset = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
}
</script>

<template>
  <div class="admin-user-page">
    <el-card class="password-card" shadow="never">
      <template #header>
        <div class="card-header">
          <div class="header-content">
            <div class="icon-wrapper">
              <el-icon class="header-icon"><Lock /></el-icon>
            </div>
            <h2>修改密码</h2>
            <p class="header-subtitle">请设置您的新密码</p>
          </div>
        </div>
      </template>

      <el-form
          ref="formRef"
          :model="passwordForm"
          :rules="rules"
          label-width="100px"
          class="password-form"
          @submit.prevent
      >
        <el-form-item label="新密码" prop="password">
          <el-input
              v-model="passwordForm.password"
              type="password"
              placeholder="请输入新密码"
              show-password
              :prefix-icon="Lock"
              class="custom-input"
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
              v-model="passwordForm.confirmPassword"
              type="password"
              placeholder="请再次输入新密码"
              show-password
              :prefix-icon="Lock"
              class="custom-input"
          />
        </el-form-item>

        <el-form-item>
          <div class="form-buttons">
            <el-button
                type="primary"
                @click="handleSubmit"
                :loading="loading"
                class="submit-btn"
                size="large"
            >
              <el-icon v-if="!loading"><Lock /></el-icon>
              {{ loading ? '修改中...' : '确认修改' }}
            </el-button>
            <el-button
                @click="handleReset"
                class="reset-btn"
                size="large"
            >
              重置
            </el-button>
          </div>
        </el-form-item>
      </el-form>

      <div class="password-tips">
        <div class="tips-header">
          <el-icon class="tips-icon"><InfoFilled /></el-icon>
          <h3>密码修改说明</h3>
        </div>
        <ul class="tips-list">
          <li><span class="tip-bullet">•</span>密码长度至少6位</li>
          <li><span class="tip-bullet">•</span>建议使用字母、数字和特殊字符的组合</li>
          <li><span class="tip-bullet">•</span>请妥善保管您的新密码</li>
        </ul>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.admin-user-page {
  padding: 20px;
  min-height: 100vh;
  background: linear-gradient(120deg, #e4ecfb 0%, #f5f7fa 100%);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  position: relative;
  overflow: hidden;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.floating-shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 80px;
  height: 80px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.shape-2 {
  width: 120px;
  height: 120px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.shape-3 {
  width: 60px;
  height: 60px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.password-card {
  width: 100%;
  max-width: 650px;
  margin-top: 0px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
  transition: all 0.3s ease;
}

.password-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
}

.card-header {
  padding: 0;
  border-bottom: none;
}

.header-content {
  text-align: center;
  padding: 20px 0;
}

.icon-wrapper {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  margin-bottom: 15px;
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.header-icon {
  font-size: 24px;
  color: white;
}

.card-header h2 {
  margin: 0 0 8px 0;
  font-size: 1.8em;
  color: #2c3e50;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.header-subtitle {
  margin: 0;
  color: #7f8c8d;
  font-size: 0.95em;
  font-weight: 400;
}

.password-form {
  padding: 30px 0;
}

.custom-input {
  transition: all 0.3s ease;
}

.custom-input:hover {
  transform: translateY(-2px);
}

.form-buttons {
  display: flex;
  gap: 15px;
  justify-content: center;
  margin-top: 20px;
}

.submit-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 12px;
  padding: 12px 30px;
  font-weight: 500;
  letter-spacing: 0.5px;
  transition: all 0.3s ease;
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 25px rgba(102, 126, 234, 0.4);
}

.reset-btn {
  border: 2px solid #e1e8ed;
  border-radius: 12px;
  padding: 12px 30px;
  font-weight: 500;
  color: #7f8c8d;
  transition: all 0.3s ease;
  background: white;
}

.reset-btn:hover {
  border-color: #667eea;
  color: #667eea;
  transform: translateY(-2px);
}

.password-tips {
  margin-top: 40px;
  padding: 25px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 15px;
  border: 1px solid #e9ecef;
}

.tips-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.tips-icon {
  color: #667eea;
  font-size: 18px;
  margin-right: 8px;
}

.password-tips h3 {
  font-size: 1.1em;
  color: #2c3e50;
  margin: 0;
  font-weight: 600;
}

.tips-list {
  color: #5a6c7d;
  padding-left: 0;
  margin: 0;
  list-style: none;
}

.tips-list li {
  margin: 12px 0;
  display: flex;
  align-items: flex-start;
  line-height: 1.6;
}

.tip-bullet {
  color: #667eea;
  font-weight: bold;
  margin-right: 10px;
  font-size: 1.2em;
}

:deep(.el-input__wrapper) {
  border-radius: 12px;
  border: 2px solid #e1e8ed;
  transition: all 0.3s ease;
  background: white;
}

:deep(.el-input__wrapper:hover) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

:deep(.el-form-item__label) {
  font-weight: 600;
  color: #2c3e50;
  font-size: 0.95em;
}

:deep(.el-card__header) {
  border-bottom: none;
  padding: 0;
}

:deep(.el-card__body) {
  padding: 0 30px 30px;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .admin-user-page {
    padding: 10px;
  }

  .password-card {
    margin-top: 20px;
    border-radius: 15px;
  }

  .form-buttons {
    flex-direction: column;
    gap: 10px;
  }

  .form-buttons .el-button {
    width: 100%;
  }

  .header-icon {
    font-size: 20px;
  }

  .card-header h2 {
    font-size: 1.5em;
  }

  :deep(.el-card__body) {
    padding: 0 20px 20px;
  }
}

@media (max-width: 480px) {
  .password-card {
    margin-top: 10px;
  }

  .icon-wrapper {
    width: 50px;
    height: 50px;
  }

  .header-icon {
    font-size: 18px;
  }
}
</style>
